package org.ling.xuan.pannel;

import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.table.JBTable;
import org.ling.xuan.entity.mo.UploadRecord;
import org.ling.xuan.table.UploadRecordTableModel;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class UploadListPanel extends JBPanel {
    private JBTable table;
    private JButton deleteButton;

    public UploadListPanel(List<UploadRecord> uploadRecordList) {
        initializeUI(uploadRecordList);
    }

    private void initializeUI(List<UploadRecord> uploadRecordList) {
        this.setLayout(new BorderLayout());

        // 创建表格模型
        UploadRecordTableModel tableModel = new UploadRecordTableModel(uploadRecordList);
        table = new JBTable(tableModel);

        // 隐藏 id 列
        table.getColumnModel().getColumn(0).setWidth(0);
        table.getColumnModel().getColumn(0).setMinWidth(0);
        table.getColumnModel().getColumn(0).setMaxWidth(0);

        // 创建滚动面板，并将表格添加到滚动面板中
        JBScrollPane scrollPane = new JBScrollPane(table);

        // 创建删除按钮
        deleteButton = new JButton("删除选中");
        deleteButton.addActionListener(e -> {
            int selectedRow = table.getSelectedRow();
            if (selectedRow != -1) {
                tableModel.removeRow(selectedRow);
            }
        });

        // 将滚动面板和删除按钮添加到面板中
        this.add(scrollPane, BorderLayout.CENTER);
        this.add(deleteButton, BorderLayout.SOUTH);
    }
}
